﻿using Lg.Core;
using Lg.Core.Extensions;
using Lg.Core.Validate;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Snowflake.Core;
using Survey.Models;
using Survey.Models.Dto;
using Survey.Models.Where;

namespace Survey.Web.Controllers
{
    [ApiController]
    [Route("api/user/[action]")]
    public class UserApiController(IDbContextFactory<SurveyDbContext> dbContextFactory, IMemoryCache cache, IdWorker idWorker) : Controller
    {
        readonly IMemoryCache _cache = cache;
        readonly IdWorker _idWorker = idWorker;
        readonly IDbContextFactory<SurveyDbContext> _dbContextFactory = dbContextFactory;
        readonly RtMsg _rtmsg = new() { code = 100, msg = "未知错误" };
        /// <summary>
        /// 获取指定学生的课程列表，学生姓名必填
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<RtMsg> GetCourseEvaluate([FromQuery] CourseEvaluateWhere where)
        {
            return await Task.Run(() =>
            {
                where.StudentName.Requird();
                using var dbContext = _dbContextFactory.CreateDbContext();
                var query = where.Process(dbContext.CourseEvaluate, out int count);
                _rtmsg.code = 0;
                _rtmsg.msg = "Success";
                _rtmsg.data = query.ToList();
                _rtmsg.count = count;
                return _rtmsg;
            });
        }

        /// <summary>
        /// 提交单个课程的评分，只更新x\y数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<RtMsg> SubmitCourseEvaluate(CourseEvaluateEditModel model)
        {
            return await Task.Run(() =>
            {
                model.XValue.Requird();
                model.YValue.Requird();
                model.Id.Requird();
                using var dbContext = _dbContextFactory.CreateDbContext();
                var dbModel = dbContext.CourseEvaluate.FirstOrDefault(x => x.Id == model.Id);
                if (dbModel != null)
                {
                    dbModel.XValue=model.XValue;
                    dbModel.YValue=model.YValue;
                    int i = dbContext.SaveChanges();
                    if (i > 0)
                    {
                        _rtmsg.code = 0;
                        _rtmsg.msg = "Success";
                    }
                }
                else
                {
                    _rtmsg.msg = "目标不存在！";
                }
                return _rtmsg;
            });
        }
        /// <summary>
        /// 获取年级去重数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<RtMsg> GetGrades()
        {
            return await Task.Run(() =>
            {
                using var dbContext = _dbContextFactory.CreateDbContext();
                var grades = dbContext.CourseEvaluate.GroupBy(x => x.Grade).Select(g => new { Grade = g.Key }).ToList();
                _rtmsg.code = 0;
                _rtmsg.data = grades;
                _rtmsg.msg = "success";
                _rtmsg.count = grades.Count;
                return _rtmsg;
            });
        }
        /// <summary>
        /// 获取年级下属班级的所有去重数据
        /// </summary>
        /// <param name="grade"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<RtMsg> GetClass(string grade)
        {
            return await Task.Run(() =>
            {
                using var dbContext = _dbContextFactory.CreateDbContext();
                var data = dbContext.CourseEvaluate.Where(x => x.Grade == grade && x.Class != null && x.Class != "").GroupBy(x => x.Class).Select(x => new { Class = x.Key }).ToList();
                _rtmsg.code = 0;
                _rtmsg.msg = "success";
                _rtmsg.data = data;
                _rtmsg.count = data.Count;
                return _rtmsg;
            });
        }
        /// <summary>
        /// 根据年级班级获取学生去重数据
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<RtMsg> GetStudent([FromQuery] GradeWhere where)
        {
            return await Task.Run(() =>
            {
                using var dbContext = _dbContextFactory.CreateDbContext();
                var data = where.Process(dbContext.CourseEvaluate, out int count).GroupBy(x => x.StudentName).Select(x => new { StudentName = x.Key }).ToList();
                _rtmsg.code = 0;
                _rtmsg.msg = "success";
                _rtmsg.data = data;
                _rtmsg.count = data.Count;
                return _rtmsg;
            });
        }
    }
}
